package com.qingge.springboot.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.*;
import com.qingge.springboot.mapper.OrdersMapper;
import com.qingge.springboot.service.ICartService;
import com.qingge.springboot.service.IGoodsService;
import com.qingge.springboot.service.IOrdersGoodsService;
import com.qingge.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IOrdersService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黎俊
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private IOrdersService ordersService;
    @Resource
    private IGoodsService goodsService;
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private IOrdersGoodsService ordersGoodsService;
    @Resource
    private ICartService cartService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Orders orders) {
        if(orders.getId() ==null){
            Date date = new Date();
            orders.setTiem(DateUtil.formatDateTime(date));
            orders.setNo(DateUtil.format(date,"yyyyMMdd")+System.currentTimeMillis());
            orders.setUserId(TokenUtils.getCurrentUser().getId());
            //创建订单
            ordersService.save(orders);
            List<Cart> carts = orders.getCarts();
            for (Cart cart:carts){
                OrdersGoods ordersGoods = new OrdersGoods();
                ordersGoods.setGoodsId(cart.getGoodsId());
                ordersGoods.setNum(cart.getNum());
                ordersGoods.setOrderId(cart.getId());
                ordersGoodsService.save(ordersGoods);
                //删除购物车
                cartService.removeById(cart.getId());
            }
        }else {
            ordersService.saveOrUpdate(orders);
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        ordersService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ordersService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(ordersService.list());
    }
    @GetMapping("/getGoodsById/{id}")
    public Result getGoodsById(@PathVariable Integer id) {
        QueryWrapper<OrdersGoods> queryWrapper = new QueryWrapper<>();
         queryWrapper.eq("order_id",id);
        ArrayList<Goods> goodsList = new ArrayList<>();
        List<OrdersGoods> ordersGoodsList = ordersGoodsService.list(queryWrapper);
        for (OrdersGoods ordersGoods: ordersGoodsList){
            Integer goodsId = ordersGoods.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            goods.setNum(ordersGoods.getNum() );
            goodsList.add(goods);
        }
        return Result.success(goodsList);
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(ordersService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                              @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
                if (!"".equals(name)) {
            queryWrapper.like("username", name);
        }
        User currentUser = TokenUtils.getCurrentUser();
//        if (!"".equals(email)) {
//            queryWrapper.like("email", email);
//        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }
        return Result.success(ordersMapper.page(new Page<>(pageNum, pageSize), name,currentUser.getRole(),currentUser.getId()));
    }

}

